package com.rms.server.Dao;

import com.rms.server.Domain.Ingredient;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface IngredientMapper {

    @Insert("INSERT INTO Ingredient (ingredient_name, unit, stock_quantity, unit_price) " +
            "VALUES (#{ingredientName}, #{unit}, #{stockQuantity}, #{unitPrice})")
    @Options(useGeneratedKeys = true, keyProperty = "ingredientId")
    int insertIngredient(Ingredient ingredient);

    @Delete("DELETE FROM Ingredient WHERE ingredient_id = #{ingredientId}")
    int deleteIngredient(Long ingredientId);

    @Update("UPDATE Ingredient SET ingredient_name = #{ingredientName}, unit = #{unit}, " +
            "stock_quantity = #{stockQuantity}, unit_price = #{unitPrice} WHERE ingredient_id = #{ingredientId}")
    int updateIngredient(Ingredient ingredient);

    @Select("SELECT ingredient_id AS ingredientId, ingredient_name AS ingredientName, unit, stock_quantity AS stockQuantity, unit_price AS unitPrice FROM Ingredient WHERE ingredient_id = #{ingredientId}")
    Ingredient selectIngredientById(Long ingredientId);

    @Select("SELECT ingredient_id AS ingredientId, ingredient_name AS ingredientName, unit, stock_quantity AS stockQuantity, unit_price AS unitPrice FROM Ingredient")
    List<Ingredient> selectAllIngredients();

    //更新库存
    @Update("UPDATE Ingredient SET stock_quantity = stock_quantity - #{quantity} WHERE ingredient_id = #{ingredientId}")
    int updateStock(@Param("ingredientId") Long ingredientId, @Param("quantity") Double quantity);
}